<?php

namespace app\common\model\banxi\sys;

use app\common\enums\AcceptAccountTypeEnum;
use app\common\model\banxi\organization\Organization;
use app\common\model\banxi\student\Student;
use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * Attachment模型
 * @property string url              文件物理路径
 * @property int quote            上传(引用)次数
 * @property int last_upload_time 最后上传时间
 */
class SysNotificationAccept extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_sys_notification_accept';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'accountTypeCn',
        'accountName'
    ];
    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'notificationId' => 'string',
        'studentId' => 'string',
        'trainerId' => 'string',
        'isRead' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s'
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                        ｜
    //-----------------------------------------------------------------------------------

    public function getAccountTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['accountType'] ?? '');
        return (new AcceptAccountTypeEnum())->getDescription($value) ?? '';
    }

    public function getAccountNameAttr($value, $data)
    {
        $value = $value ?: ($data['accountId'] ?? '');
        $accountType = $data['accountType'] ?? '';
        switch ($accountType) {
            case AcceptAccountTypeEnum::ORGAN:
                $organizationModel = new Organization();
                if ($value == 0) {
                    $organizationInfo = $organizationModel->getOrganizationInfo();
                    $organizationName = $organizationInfo['name'];
                } else {
                    $organizationName = $organizationModel->where('id', $value)->cache('organ_name_' . $value, 30)->value('name');
                }
                return "【机构】" . $organizationName ?? '已删除';
            case AcceptAccountTypeEnum::TRAINER:
                $trainerName = Trainer::where('id', $value)->cache('trainer_name_' . $value, 30)->value('name') ?? '已删除';
                return "【教练】" . $trainerName;
            case AcceptAccountTypeEnum::STUDENT:
                $studentName = Student::where('id', $value)->cache('student_name_' . $value, 30)->value('name') ?? '已删除';
                return "【学员】" . $studentName;
            default:
                return "【所有】";
        }
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    /**
     * 标记通知为已读
     *
     * @param int $notificationId 通知ID
     * @param string $accountId 账户ID
     * @param string $accountType 账户类型
     * @return void
     */
    public function markRead($notificationId, $accountId, $accountType)
    {
        // 检查是否已读
        $isRead = $this->getIsReadByAccountId($notificationId, $accountId, $accountType);

        if (!$isRead || !$isRead['isRead']) {
            // 标记为已读
            $notificationAccept = [
                'notificationId' => $notificationId,
                'accountId'      => $accountId,
                'accountType'    => $accountType,
                'isRead'         => 1,
            ];

            // 根据账户类型填充相应字段
            array_merge($notificationAccept, $this->accountFill($accountType, $accountId));
            // 存在记录则更新，否则新增
            if ($isRead) {
                $this->where('id', $isRead['id'])->update($notificationAccept);
            } else {
                $notificationAccept['createTime'] = time();
                $notificationAccept['updateTime'] = time();
                $this->save($notificationAccept);
            }

        }
    }

    /**
     * 根据账户ID获取是否已读状态
     *
     * @param int $notificationId 通知ID
     * @param string $accountId 账户ID
     * @param string $accountType 账户类型
     * @return array|null
     */
    public function getIsReadByAccountId($notificationId, $accountId, $accountType)
    {
        return $this->where('notificationId', $notificationId)
            ->where('accountId', $accountId)
            ->where('accountType', $accountType)
            ->find();
    }


    public function accountFill($accountType, $accountId)
    {
        $data = [];
        switch ($accountType) {
            case AcceptAccountTypeEnum::ORGAN:
                $data['organizationId'] = $accountId;
                break;
            case AcceptAccountTypeEnum::TRAINER:
                $data['trainerId'] = $accountId;
                break;
            case AcceptAccountTypeEnum::STUDENT:
                $data['studentId'] = $accountId;
                break;
        }
        return $data;
    }


    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}